const app = getApp()
Page({
    data: {
        pageNane:'examine',
        classification: false,
        tokenID: '',
        page: 1,
        moment: [],
        datamsg: false,
        classify: '全部',
        classifyid: 'ALL'
    },
    onShow:function(){
        var _this = this;
        _this.setData({
            tokenID: app.data.tokenID
        })
        _this.onReachBottom();
      
    },
    onPullDownRefresh: function() {
        var _this = this;
        this.setData({
            page: 1
        })
        wx.showNavigationBarLoading();
        _this.gtemomentList("down")
    },
    onReachBottom: function() {
        var that = this;
        that.gtemomentList("up")
    },
    gtemomentList: function(type) {
        wx.showLoading({
            title: '玩命加载中',
        })
        var that = this;
        that.setData({
            tokenID: app.data.tokenID
        })
        wx.request({
            url: app.data.api + 'exam/getexamplan',
            data: {
                "page": that.data.page,
                "token": that.data.tokenID,
                "type": that.data.classifyid
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.status) {
                    if (type == 'up') {
                        var moment_list = that.data.moment;
                        for (var i = 0; i < res.data.data.list.length; i++) {
                            moment_list.push(res.data.data.list[i]);
                        }
                        if (res.data.data.list.length == 0) {
                            that.setData({
                                datamsg: true
                            })
                        }
                        that.setData({
                            page: that.data.page + 1,
                            // moment: that.data.JSON.parse(str)
                            moment: moment_list
                        })
                    } else {
                        var moment_list = [];

                        for (var i = 0; i < res.data.data.list.length; i++) {

                            moment_list.push(res.data.data.list[i]);
                        }
                        that.setData({
                            page: that.data.page + 1,
                            moment: moment_list
                        })
                        wx.stopPullDownRefresh();
                    }
                    wx.hideLoading();
                    wx.hideNavigationBarLoading();
                } else {
                    wx.hideLoading();
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        success: function(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            } else if (res.cancel) {
                                wx.redirectTo({
                                    url: '../login/login'
                                })
                            }
                        }
                    });
                }
            }
        })
    },
    showClassification: function() {
        this.setData({
            classification: true
        })
    },
    hideClassification: function() {
        this.setData({
            classification: false
        })
    },
    search: function(event) {
        var _this = this,
            id = event.currentTarget.dataset.id,
            name = event.currentTarget.dataset.name;
        _this.setData({
            classify: name,
            classifyid: id,
            classification: false,
            page: 1
        })
        _this.gtemomentList("down")
    },
    linkpage: function(event) {
        var _this = this,
            euaid = event.currentTarget.dataset.euaid,
            title = event.currentTarget.dataset.title;
        wx.request({
            url: app.data.api + 'exam/checkExam',
            data: {
                "euaId": euaid,
                "token": _this.data.tokenID
            },
            method: "POST",
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.data.event == 'alert') {
                    wx.showModal({
                        title: '提示',
                        showCancel: false,
                        confirmColor: '#04A9F4',
                        content: res.data.data.message,
                        success: function(res) {
                            if (res.confirm) {
                            }
                        }
                    })
                } else if (res.data.data.event == 'toexam') {
                    wx.navigateTo({
                        url: '../testPaper/testPaper?title=' + title + '&euaid=' + euaid
                    })
                } else if (res.data.data.event == 'showexam') {
                    wx.navigateTo({
                        url: '../testPreview/testPreview?title=' + title + '&euaid=' + euaid
                    })
                }
            }
        })
    },
    onUnload() {
        const wxCurrPage = getCurrentPages();//获取当前页面的页面栈
        const wxPrevPage = wxCurrPage[wxCurrPage.length - 2];//获取上级页面的page对象
        console.log(wxPrevPage.data.pageNane === "testPaper")
        if (wxPrevPage.data.pageNane === "testPaper") {
            wx.reLaunch({
                url: '../index/index'
            })
        }
    }
})